from flask import Flask
from flask_cors import CORS
from flask_restful import Api

from app.hrs.views import DeptAPI, DeptListAPI, EmpListAPI
from app.models import db
from config import config


def create_app(config_key='default'):
    app = Flask(__name__)
    app.config.from_object(config[config_key])
    db.init_app(app)
    CORS(app, resources={r'/api/*': {'origins': '*'}})
    api = Api(app)
    api.add_resource(DeptListAPI, '/api/depts')
    api.add_resource(DeptAPI, '/api/depts/<int:no>')
    api.add_resource(EmpListAPI, '/api/emps')
    return app
